package org.gensokyo.shanbay

import java.time.{LocalDate, LocalDateTime}

import com.typesafe.scalalogging.slf4j.StrictLogging

import scala.actors._
import scala.collection.mutable.ListBuffer

/**
 * Created by liusen on 15-11-29.
 */
object ShanBayClockOnTimer extends Actor with StrictLogging {

  val dateList: ListBuffer[LocalDate] = ListBuffer.empty[LocalDate]

  val clockHour: Int = 23

  override def act(): Unit = {
    loop {
      val dateNow = LocalDate.now()
      val dateTimeNow = LocalDateTime.now()
      if (!dateList.contains(dateNow) && dateTimeNow.getHour >= clockHour) {
        logger.debug("start timer task")
        logger.debug(s"add dateNow:${dateNow} to dateList")
        dateList.append(dateNow)
        ShanBayClockOn.deal()
      } else {
        logger.debug("Timer waiting for handle task")
        Thread.sleep(5 * 60 * 1000L)
      }
    }
  }

  override def exceptionHandler = {
    case e: Exception => logger.error(e.getMessage, e)
  }

}
